﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using EntityManage;

public partial class Modules_Default_MyNotice : System.Web.UI.Page
{
    ISys_UserHandler userHandler = new Sys_UserManage();
    ISys_OrganizationHandler orgHandler = new Sys_OrganizationManage();
    IOther_NoticeHandler noticeHandler = new Other_NoticeManage();
    IOther_NoticeAttachHandler noticeAttHandler = new Other_NoticeAttachManage();
    IOther_NoticeCommentHandler noticeCommentHandler = new Other_NoticeCommentManage();
    IPerf_Personal_ApprovalHandler approvalHandler = new Perf_Personal_ApprovalManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["OrderBy"] = "Id";
            ViewState["Accending"] = "asc";

            dataBind(" 1=1 ", new SqlParameter[]{});
          
        }
    }

    /// <summary>
    /// 初始化岗位信息表
    /// </summary>
    private void dataBind(string whereClause, SqlParameter[] parms)
    {
        try
        {
            // 读取各上级单位
            int userId = int.Parse(Session["CurrentUserID"].ToString());
            Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(userId);
            string orgIds = userQuery.Org_ID.ToString();
            Sys_OrganizationQuery orgQuery = orgHandler.GetSys_OrganizationQuery(userQuery.Org_ID);
            for (int i = 0; i < 50; i++)
            {
                if (orgQuery.Parent_ID != 0)
                {
                    orgIds += "," + orgQuery.Parent_ID.ToString();
                    orgQuery = orgHandler.GetSys_OrganizationQuery(orgQuery.Parent_ID);
                }
                else
                {
                    break;
                }
            }
            if (userQuery.Org_ID2 != 0)
            {
                orgIds += "," + userQuery.Org_ID2.ToString();
                orgQuery = orgHandler.GetSys_OrganizationQuery(userQuery.Org_ID2);
                for (int i = 0; i < 50; i++)
                {
                    if (orgQuery.Parent_ID != 0)
                    {
                        orgIds += "," + orgQuery.Parent_ID.ToString();
                        orgQuery = orgHandler.GetSys_OrganizationQuery(orgQuery.Parent_ID);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            whereClause += " and Notice_Range in (" + orgIds + ")";
            whereClause += " and convert(varchar(10), Period,121) >= '" + DateTime.Now.ToString("yyyy-MM-dd") + "' and Status = 0";
            //parms = new SqlParameter[] { new SqlParameter("@Status", 0) };

            DataTable dt = noticeHandler.GetOther_NoticeList(whereClause, parms);

            gvNoticeList.DataSource = dt;
            gvNoticeList.DataBind();
        }
        catch
        {
            Jscript.AlertInAjax("連接錯誤!", UpdatePanel1);
            return;
        }
    }

    protected void gvNoticeList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex > -1)
            {
                Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(int.Parse(e.Row.Cells[1].Text));
                if(userQuery != null)
                    e.Row.Cells[1].Text = userQuery.Name;

                Other_NoticeQuery noticeQuery = noticeHandler.GetOther_NoticeQuery(int.Parse(e.Row.Cells[5].Text));
                Perf_Personal_ApprovalQuery approvalQuery = approvalHandler.GetPerf_Personal_ApprovalQuery(noticeQuery.Business_ID);
                int userId = int.Parse(Session["CurrentUserID"].ToString());
                ImageButton btnAppeal = (ImageButton)e.Row.FindControl("ibtnAppeal");
                if (noticeQuery != null && noticeQuery.Type == (int)Enums.OtherNoticeType.PersonalPerfNotice
                    && approvalQuery != null && approvalQuery.Target_User_ID == userId)
                    btnAppeal.Visible = true;
                else
                    btnAppeal.Visible = false;

                // 附件数
                int attNum = noticeAttHandler.GetCounter(" Notice_ID = @Notice_ID", new SqlParameter[] { new SqlParameter("@Notice_ID", int.Parse(e.Row.Cells[5].Text)) });
                e.Row.Cells[5].Text = attNum.ToString();

                // 评论数
                int commentNum = noticeCommentHandler.GetCounter(" Notice_ID = @Notice_ID", new SqlParameter[] { new SqlParameter("@Notice_ID", int.Parse(e.Row.Cells[6].Text)) });
                e.Row.Cells[6].Text = commentNum.ToString();
            }
        }
    }

    protected void gvNoticeList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //
        if (e.CommandName == "view")
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString().Trim());
            try
            {
                
            }
            catch
            {
                Jscript.AlertInAjax("操作失败!", UpdatePanel1);
            }
        }
    }

}
